from typing import List


class Solution:
    def sumDigitDifferences(self, nums: List[int]) -> int:
        newNums = []
        for index, num in enumerate(nums):
            i = 0
            while num != 0:
                n = num % 10
                num = int(num / 10)
                if index == 0:
                    v = [0] * 10
                    v[n] += 1
                    newNums.append(v)
                else:
                    newNums[i][n] += 1
                i = i + 1
        all = 0
        for num in newNums:
            sum = 0
            sameSum = 0
            for i, count in enumerate(num):
                sum += count
                if count > 1:
                    sameSum += count * (count - 1)
            all += (sum * (sum - 1) - sameSum) / 2
            # print(num)
            # print(sameSum)
            # print(sum)
            # print(sum * (sum - 1) - sameSum)
        return int(all)


if __name__ == '__main__':
    nums = [10, 10, 10, 10]
    s = Solution()

    print(s.sumDigitDifferences(nums))
